package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import dao.ArticleDAO;
import dao.ContentDAO;
import dao.SensitiveWordDAO;
import entity.Content;
import entity.User;

/**
 * Servlet implementation class UeditorServlet
 */
@WebServlet("/AddArticleServlet")
public class AddArticleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ArticleDAO articleDAO = ArticleDAO.getInstance();
	private ContentDAO  contentDAO= ContentDAO.getInstance();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddArticleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		try{
			//session.getAttribute(arg0)
			HttpSession session = request.getSession(true);
			User login_user =(User)session.getAttribute("user");
			String content = (String)request.getParameter("editorValue");
			SensitiveWordDAO sl = SensitiveWordDAO.getInstance();
			content = sl.Sensitive(content);
			int theme_id = Integer.parseInt((String)request.getParameter("selected_theme"));//选择的主题
			String article_title = (String)request.getParameter("Article_title");//帖子名
			
			article_title = sl.Sensitive(article_title);
			System.out.println(article_title);
			//Content content = new Content(2,"aaaaaaaaa");
			//contentDAO.add(content);
			
			int article_id=articleDAO.add(article_title,login_user.getUser_id(),theme_id,content);//添加了帖子到数据库
			System.out.println("article的id"+article_id);
			request.setAttribute("article_title", article_title);
			request.setAttribute("article_id", article_id);
			request.setAttribute("type",1);//Add的type设为2
			request.getRequestDispatcher("AfterAdd_success.jsp").forward(request, response);
		}catch(Exception e){
			//request.getRequestDispatcher("AfterAdd_fail.jsp").forward(request, response);
			e.printStackTrace();
		}
		
		//request.getRequestDispatcher("Show.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
